package com.im.users.controller;

import com.im.common.redis.RedisKeys;
import com.im.common.utils.R;
import com.im.security.annotation.LoginUser;
import com.im.security.dto.LoginVo;
import com.im.users.service.UserService;
import com.im.users.vo.DetialVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @auther Zeng
 * @date 2023/7/13 - 11:07
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/detial/{uid}")
    public R detail(@LoginUser LoginVo loginVo, @PathVariable Integer uid) {
        System.out.println(loginVo);
        DetialVo detialVo = userService.getUserDetail(uid);
        return R.seccess(detialVo);
    }

    @PostMapping("/updateUserLocation")
    public R updateUserLocation(@RequestBody Map map,@LoginUser LoginVo loginVo) {
        Double userLongitude = Double.parseDouble(map.get("userLongitude").toString());
        Double userLatitude = Double.parseDouble(map.get("userLatitude").toString());
        Long id = loginVo.getId();
        Long result = null;
        if (userLongitude != null && userLatitude != null) {
            result = stringRedisTemplate.opsForGeo()
                    .add(RedisKeys.getUserLocationKey(),
                            new Point(userLongitude, userLatitude), id.toString());
        }
        return R.seccess(result);
    }
}
